package cn.frh.basic.util;

import cn.frh.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;
/**
 * 获取当前登录人工具类
 */
public class LoginContext {
    /*@Autowired
    private RedisTemplate redisTemplate;*/

    public static Logininfo getCurrentLogininfo(HttpServletRequest request){
        String token = request.getHeader("token");
        if (token==null){
            return null;
        }
        //获取容器
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        //获取对象
        RedisTemplate redisTemplate = (RedisTemplate) webApplicationContext.getBean("redisTemplate");
        Object logininfo = redisTemplate.opsForValue().get(token);
        if (logininfo==null){
            return null;
        }
        return (Logininfo) logininfo;
    }
}
